package com.example2016.cjj.wxplangamedemo.game;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import com.example2016.cjj.wxplangamedemo.view.GameView;

/**
 * Created by CJJ on 2017/3/27.
 */

public class Explosion extends Sprite {
    private int segment = 14; //爆炸效果由14个片段组成

    private int level = 0; //最开始处于爆炸的0片段

    private int explodeFrequency = 2; //每隔爆炸效果绘制2帧


    public Explosion(Bitmap bitmap) {
        super(bitmap);
    }


    @Override
    public float getWidth() {
        Bitmap bitmap = getBitmap();
        if( bitmap!=null ){
            return bitmap.getWidth() / segment;
        }
        return 0;
    }


    @Override
    public Rect getBitmapSrcRect() {
        Rect rect = super.getBitmapSrcRect();
        int left = (int) (level* getWidth()); //计算每隔爆炸片段的偏移量
        rect.offsetTo(left,0);
        return rect;
    }


    @Override
    protected void afterDraw(Canvas canvas, Paint paint, GameView gameView) {
        if( !isDestroy() ){
            if( getFrame() % explodeFrequency ==0 ){
                //每绘制2帧，换下一个爆炸片段
                level ++;
                if( level >= segment ){
                    //绘制完了所有爆炸片段，就销毁
                    destroy();
                }

            }
        }
    }




}

